//
//  EFReturnDetailViewController.m
//  EnjogFood
//
//  Created by 王芳 on 17/6/10.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFReturnDetailViewController.h"
#import "EFReturnDetailItem.h"
#import "EFOrderStatusItem.h"
#import "EFOrderStatusCell.h"

#import "EFReturnInforItem.h"
#import "EFReturnInfoCell.h"

#import "EFBtnTitleItem.h"
#import "EFReturnBtnCell.h"

#import "EFSeporatorItem.h"
#import "EFSeperatorCell.h"

#import "EFReturnFoldUnFoldItem.h"
#import "EFReturnFolderUnFolerCell.h"

#import "EFReturnProductItem.h"
#import "EFOrderProductCell.h"

#import "EFReturnTotalPriceItem.h"
#import "EFReturnTotalPriceCell.h"

#import "EFReturnReasonItem.h"
#import "EFReturnReasonCell.h"

#import "iToast.h"

@interface EFReturnDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (strong, nonatomic) NSString *returnId;

@property (strong, nonatomic) EFReturnDetailItem *returnDetailItem;

@property (strong, nonatomic) UITextField *saveCarricTextField;
@property (strong, nonatomic) UITextField *transNoTextField;
@end

@implementation EFReturnDetailViewController

- (instancetype)initWithReturnId:(NSString *)returnId{
    self = [super init];
    if (self) {
        self.dataArray = [NSMutableArray array];
        self.title = @"退货换货详情";
        self.returnId = returnId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
    [self requestReturnInf];
    
}

- (void)back:(id)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)requestReturnInf{
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getReturnInfor:self.returnId OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        self.HUD.hidden = YES;
        if (code == 0) {
            weakSelf.returnDetailItem = [EFReturnDetailItem yy_modelWithDictionary:info];
            [weakSelf armyArray];
        }else{
            [[iToast makeText:message] show];
        }
    }];
}
// 0待审核  1 申请审核通过  2 申请审核拒绝   3 已完结  4 处理中
- (void)armyArray{
    EFOrderStatusItem *returnStatusItem = [[EFOrderStatusItem alloc] init];
    returnStatusItem.orderStatus = self.returnDetailItem.statusName;
    [self.dataArray addObject:returnStatusItem];

    if (self.returnDetailItem.status.integerValue == 0) {//待审核
        EFReturnInforItem *applyTimeItem = [[EFReturnInforItem alloc] init];
        applyTimeItem.title = @"申请时间：";
        applyTimeItem.detail = self.returnDetailItem.requestTime;
        [self.dataArray addObject:applyTimeItem];
        
        EFReturnInforItem *contactItem = [[EFReturnInforItem alloc] init];
        contactItem.title = @"联系人：";
        contactItem.detail = self.returnDetailItem.name;
        [self.dataArray addObject:contactItem];
        
        EFReturnInforItem *phoneItem = [[EFReturnInforItem alloc] init];
        phoneItem.title = @"电话：";
        phoneItem.detail = self.returnDetailItem.phone;
        [self.dataArray addObject:phoneItem];

        EFReturnInforItem *returnReasonItem = [[EFReturnInforItem alloc] init];
        returnReasonItem.title = @"退货原因：";
        returnReasonItem.detail = self.returnDetailItem.reason;
        [self.dataArray addObject:returnReasonItem];
        
        EFBtnTitleItem *btnItem = [[EFBtnTitleItem alloc] init];
        btnItem.title = @"取消";
        [self.dataArray addObject:btnItem];
    }else if(self.returnDetailItem.status.integerValue == 1){//审核通过
        EFReturnInforItem *applyTimeItem = [[EFReturnInforItem alloc] init];
        applyTimeItem.title = @"申请时间：";
        applyTimeItem.detail = self.returnDetailItem.requestTime;
        [self.dataArray addObject:applyTimeItem];

        //申请通过的reason
        EFReturnReasonItem *reasonItem = [[EFReturnReasonItem alloc] init];
        reasonItem.content = self.returnDetailItem.returnMessage.notice;
        [self.dataArray addObject:reasonItem];
        
        EFReturnInforItem *addressItem = [[EFReturnInforItem alloc] init];
        addressItem.title = @"邮寄地址：";
        addressItem.detail = [NSString stringWithFormat:@"%@\rn%@(收) %@",self.returnDetailItem.returnMessage.returnAddress,self.returnDetailItem.returnMessage.returnName,self.returnDetailItem.returnMessage.returnPhone];
        [self.dataArray addObject:addressItem];
        
        EFReturnInforItem *companyItem = [[EFReturnInforItem alloc] init];
        companyItem.title = @"快递公司：";
        companyItem.isEdit = YES;
       // companyItem.detail = self.returnDetailItem.returnMessage.returnAddress;
        [self.dataArray addObject:companyItem];
        
        EFReturnInforItem *transIdItem = [[EFReturnInforItem alloc] init];
        transIdItem.title = @"快递单号：";
        transIdItem.isEdit = YES;
       // transIdItem.detail = self.returnDetailItem.trackingNo;
        [self.dataArray addObject:transIdItem];
        
        EFBtnTitleItem *btnItem = [[EFBtnTitleItem alloc] init];
        btnItem.title = @"保存";
        [self.dataArray addObject:btnItem];
        
    }else if(self.returnDetailItem.status.integerValue == 2){//审核拒绝
        EFReturnInforItem *applyTimeItem = [[EFReturnInforItem alloc] init];
        applyTimeItem.title = @"申请时间：";
        applyTimeItem.detail = self.returnDetailItem.requestTime;
        [self.dataArray addObject:applyTimeItem];

        EFReturnInforItem *refuseTimeItem = [[EFReturnInforItem alloc] init];
        refuseTimeItem.title = @"拒绝时间：";
        refuseTimeItem.detail = self.returnDetailItem.requestTime;
        [self.dataArray addObject:refuseTimeItem];

        //申请通过的reason
        EFReturnInforItem *refuseReason = [[EFReturnInforItem alloc] init];
        refuseReason.title = @"拒绝原因：";
        refuseReason.detail = self.returnDetailItem.returnMessage.notice;
        [self.dataArray addObject:refuseReason];
        
//        EFReturnReasonItem *reasonItem = [[EFReturnReasonItem alloc] init];
//        
//        reasonItem.content = self.returnDetailItem.returnMessage.notice;
//        [self.dataArray addObject:reasonItem];
    }else if(self.returnDetailItem.status.integerValue == 3){//已完结 退货成功了
        EFReturnInforItem *applyTimeItem = [[EFReturnInforItem alloc] init];
        applyTimeItem.title = @"退货时间：";
        applyTimeItem.detail = self.returnDetailItem.requestTime;
        [self.dataArray addObject:applyTimeItem];
        
        EFReturnInforItem *companyItem = [[EFReturnInforItem alloc] init];
        companyItem.title = @"退货快递：";
        companyItem.detail = self.returnDetailItem.carrier;
        [self.dataArray addObject:companyItem];
        
        EFReturnInforItem *transIdItem = [[EFReturnInforItem alloc] init];
        transIdItem.title = @"快递单号：";
        transIdItem.detail = self.returnDetailItem.trackingNo;
        [self.dataArray addObject:transIdItem];
        
        EFReturnInforItem *returnReasonItem = [[EFReturnInforItem alloc] init];
        returnReasonItem.title = @"退货原因：";
        returnReasonItem.detail = self.returnDetailItem.reason;
        [self.dataArray addObject:returnReasonItem];

        
    } if (self.returnDetailItem.status.integerValue == 4) {//处理中
        EFReturnInforItem *applyTimeItem = [[EFReturnInforItem alloc] init];
        applyTimeItem.title = @"申请时间：";
        applyTimeItem.detail = self.returnDetailItem.requestTime;
        [self.dataArray addObject:applyTimeItem];

        EFReturnInforItem *companyItem = [[EFReturnInforItem alloc] init];
        companyItem.title = @"快递公司：";
        companyItem.detail = self.returnDetailItem.carrier;
        [self.dataArray addObject:companyItem];
        
        EFReturnInforItem *transIdItem = [[EFReturnInforItem alloc] init];
        transIdItem.title = @"快递单号：";
        transIdItem.detail = self.returnDetailItem.trackingNo;
        [self.dataArray addObject:transIdItem];
        
        EFBtnTitleItem *btnItem = [[EFBtnTitleItem alloc] init];
        btnItem.title = @"取消";
        [self.dataArray addObject:btnItem];
    }
    EFSeporatorItem *seporItem = [[EFSeporatorItem alloc] init];
    [self.dataArray addObject:seporItem];
    
    EFReturnFoldUnFoldItem *returnHeadItem = [[EFReturnFoldUnFoldItem alloc] init];
    returnHeadItem.title = @"退货商品";
    returnHeadItem.isDown = NO;
    [self.dataArray addObject:returnHeadItem];
    
    //增加退货商品
    for (EFReturnProductItem *pItem in self.returnDetailItem.returnProduct) {
        [self.dataArray addObject:pItem];
    }
    //退货总价
    EFReturnTotalPriceItem *rpItem = [[EFReturnTotalPriceItem alloc] init];
    rpItem.totalPrice = [NSString stringWithFormat:@"%@",self.returnDetailItem.returnTotalAmount];
    [self.dataArray addObject:rpItem];
    EFSeporatorItem *sepor1Item = [[EFSeporatorItem alloc] init];
    [self.dataArray addObject:sepor1Item];
    
    if (self.returnDetailItem.exchangeProduct.count > 0) {
        //换货
        EFReturnFoldUnFoldItem *exchangeHeadItem = [[EFReturnFoldUnFoldItem alloc] init];
        exchangeHeadItem.title = @"换货商品";
        exchangeHeadItem.isDown = NO;
        [self.dataArray addObject:exchangeHeadItem];
        for (EFReturnProductItem *pItem in self.returnDetailItem.exchangeProduct) {
            [self.dataArray addObject:pItem];
        }
        
        EFReturnTotalPriceItem *epItem = [[EFReturnTotalPriceItem alloc] init];
        epItem.totalPrice = [NSString stringWithFormat:@"%@",self.returnDetailItem.exchangeTotalAmount];
        [self.dataArray addObject:epItem];
        EFSeporatorItem *sepor2Item = [[EFSeporatorItem alloc] init];
        [self.dataArray addObject:sepor2Item];
        [self.dataArray addObject:epItem];
    }
   
    if (self.returnDetailItem.otherProduct.count > 0) {
        //其他
        EFReturnFoldUnFoldItem *noHeadItem = [[EFReturnFoldUnFoldItem alloc] init];
        noHeadItem.title = @"其他";
        noHeadItem.isDown = NO;
        [self.dataArray addObject:noHeadItem];
        
        for (EFReturnProductItem *pItem in self.returnDetailItem.otherProduct) {
            [self.dataArray addObject:pItem];
        }
        
        EFReturnTotalPriceItem *opItem = [[EFReturnTotalPriceItem alloc] init];
        opItem.totalPrice = [NSString stringWithFormat:@"%@",self.returnDetailItem.otherTotalAmount];
        
        EFSeporatorItem *sepor3Item = [[EFSeporatorItem alloc] init];
        [self.dataArray addObject:sepor3Item];
    }
   
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight) style:UITableViewStylePlain];
        _tableView.dataSource= self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // for (NSInteger index = 0; index < self.dataSourceArray.count; index++) {
    NSObject *item = [self.dataArray objectAtIndex:indexPath.row];
    if ([item isKindOfClass:[EFOrderStatusItem class]]) {
        return 50;
    }
    if ([item isKindOfClass:[EFReturnInforItem class]]) {
        return [EFReturnInfoCell returnInfoCellHeight:(EFReturnInforItem *)item];
    }
    if ([item isKindOfClass:[EFBtnTitleItem class]]) {
        return 65;
    }
    if ([item isKindOfClass:[EFSeporatorItem class]]) {
        return 10;
    }
    if ([item isKindOfClass:[EFReturnFoldUnFoldItem class]]) {
        return 45;
    }
    if ([item isKindOfClass:[EFReturnProductItem class]]) {
        return 107;
    }
    if ([item isKindOfClass:[EFReturnTotalPriceItem class]]) {
        return 46;
    }
    if ([item isKindOfClass:[EFReturnReasonItem class]]) {
        return [EFReturnReasonCell returnReasonCellHeight:(EFReturnReasonItem *)item];
    }
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSObject *obj = [self.dataArray objectAtIndex:indexPath.row];
    //UITableViewCell *cell = nil;
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"empty"];
    if ([obj isKindOfClass:[EFOrderStatusItem class]]) {
        cell = [[EFOrderStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"status"];
        [(EFOrderStatusCell *)cell setObject:(EFOrderStatusItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFReturnInforItem class]]) {
        cell = [[EFReturnInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"number"];
        [(EFReturnInfoCell *)cell setObject:(EFReturnInforItem *)obj];
        if (((EFReturnInforItem *)obj).isEdit == YES) {
            if ([((EFReturnInforItem *)obj).title isEqualToString:@"快递公司："]) {
                self.saveCarricTextField = ((EFReturnInfoCell *)cell).rightTextField;
            }else if([((EFReturnInforItem *)obj).title isEqualToString:@"快递单号："]){
                self.transNoTextField = ((EFReturnInfoCell *)cell).rightTextField;
            }
        }
        return cell;
    }
    if ([obj isKindOfClass:[EFBtnTitleItem class]]) {
        cell = [[EFReturnBtnCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"transe"];
        [(EFReturnBtnCell *)cell setBtnTitle:(EFBtnTitleItem *)obj];
        //增加按钮的block
        EFBtnTitleItem *titleItem = (EFBtnTitleItem *)obj;
        if ([titleItem.title isEqualToString:@"保存"]) {
            ((EFReturnBtnCell *)cell).saveClick = ^(){
                if (self.transNoTextField.text.length == 0) {
                    [[iToast makeText:@"请填写快递单号"] show];
                    return;
                }
                if(self.saveCarricTextField.text.length == 0){
                    [[iToast makeText:@"请填写快递公司"] show];
                    return;
                }
                [APP_CONTEXT.net saveInfoTrackingNo:self.transNoTextField.text returnId:self.returnId carrier:self.saveCarricTextField.text OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
                        [[iToast makeText:@"保存成功"] show];
                    [self.transNoTextField resignFirstResponder];
                    [self.saveCarricTextField resignFirstResponder];
                    
                }];
            };
        }else if([titleItem.title isEqualToString:@"取消"]){
            ((EFReturnBtnCell *)cell).cancelBtnClick = ^(){
                [APP_CONTEXT.net cancelReturn:self.returnId OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
                    [[iToast makeText:@"取消成功"] show];
                    if (code == 0) {
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                }];
            };
        }
        return cell;
    }
    if ([obj isKindOfClass:[EFReturnFoldUnFoldItem class]]) {
        cell = [[EFReturnFolderUnFolerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"userInfo"];
        [(EFReturnFolderUnFolerCell *)cell setObject:(EFReturnFoldUnFoldItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFReturnProductItem class]]) {
        cell = [[EFOrderProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"producet"];
        [(EFOrderProductCell *)cell setReturnObject:(EFReturnProductItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFReturnTotalPriceItem class]]) {
        cell = [[EFReturnTotalPriceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"more"];
        [(EFReturnTotalPriceCell *)cell setObject:(EFReturnTotalPriceItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFReturnReasonItem class]]) {
        cell = [[EFReturnReasonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"payDetail"];
        [(EFReturnReasonCell *)cell setObject:(EFReturnReasonItem *)obj];
        return cell;
    }
    if ([obj isKindOfClass:[EFSeporatorItem class]]) {
        cell = [[EFSeperatorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"seperator"];
        return cell;
    }
   
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
